<?php
class ColorcardApp extends MemberbaseApp {
    function  __construct() {
        $this->ColorcardApp();
    }
    function ColorcardApp() {
    	parent::__construct();
    }
    
    function appove_color(){
    	header("location:index.php?app=colorcard&act=index&color_status=true");
    }
    //色卡列表及查询
    function index() {
    	$page=$_GET['page']?intval($_GET['page']):'1';
    	$perpage=30;
		$user=$this->visitor->get();
    	$start=($page-1)*$perpage;
    	$start<0 && $start=0;
    	$page = $this->_get_page($perpage);
    	$color_status=isset($_GET['color_status'])?$_GET['color_status']:'';
    	//print_r($page);
    	$fs_mod =& m('fabric_shop');
		$member_mod =& m('member');
		$colorcard_mode=&m('color_card');
    	$this->assign('fabricshop_list', $fs_mod->find(array('conditions'=>'if_show=1','order'=>'fabric_shop_name ASC')));
		$this->assign('cl_item_list', $colorcard_mode->getall("select item from {$colorcard_mode->table} group by item order by item asc"));
		$this->assign('cl_user_list',$colorcard_mode->getall("select cc.user_id,user_name from {$colorcard_mode->table} cc left join {$member_mod->table} m on cc.user_id=m.user_id group by user_id order by user_id asc"));
    	$this->_curlocal(Lang::get('color_card'));
    	$color_rel_mod=&m('color_relation');
    	//显示面料特征
    	$color_char_mod=&m('color_property');
    	$color_char_list=$color_char_mod->find(array(
    			'conditions'=>'1=1',
    			'order'=>'cf_time desc'
    	));
    	
    	//显示颜色属性
    	$color_attr_mod=&m('color_attr');
    	$color_attr_list=$color_attr_mod->find(array(
    			'conditions'=>'1=1',
    			'order'=>'cs_time desc'
    	));
    	$this->assign('color_char_list',$color_char_list);
    	$this->assign('color_attr_list',$color_attr_list);
    	
    	$data=array(
    			'price1' => $_REQUEST['price1']?floatval($_REQUEST['price1']):'0.00',
    			'fabric_shop_id' => $_REQUEST['fabric_shop_id']?intval($_REQUEST['fabric_shop_id']):'0',
    			'price2' => $_REQUEST['price2']?floatval($_REQUEST['price2']):'0.00',
    			'fabric_characteristics' => $_REQUEST['fabric_characteristics']?trim($_REQUEST['fabric_characteristics']):'',
				'user_id' => $_REQUEST['user_id']?intval($_REQUEST['user_id']):'0',
				'item' => $_REQUEST['item']?trim($_REQUEST['item']):'',
    	);
    	$order = empty($_GET['order']) ? 'update_time DESC' : str_replace('-', ' ', trim($_GET['order']));
    	if ($order != 'update_time DESC' || $order != 'update_time ASC') {
    		$order .= ', update_time DESC';
    	}
    	//print_r($data);
    	$condition = "1=1";
    	if ($color_status){
    		$condition .= " AND color_status=0";
    		if(in_array($this->visitor->get('role'), array(USER_MERCHANDISER)))
    		{
    			$condition .= " AND cc.user_id=".$this->visitor->get('user_id');
    		}
    		$this->assign('color_status',$color_status);
    	}
    	if($data['price1']!=0){
    		$condition .= " AND price>=".$data['price1'];
    	}
    	if($data['price2']!=0){
    		$condition .= " AND price<=".$data['price2'];
    	}
    	if($data['fabric_shop_id']){
    		$condition .= " AND fabric_shop_id=".$data['fabric_shop_id'];
    	}
//     	if($data['fabric_characteristics']){
//     		$condition .= " AND fabric_characteristics LIKE '%".$data['fabric_characteristics']."%'";
//     	}
		if($data['user_id']){
    		$condition .= " AND cc.user_id =".$data['user_id'];
    	}
		if($data['item']){
    		$condition .= " AND item like '%".$data['item']."%'";
    	}
		if(in_array($this->visitor->get('role'), array(USER_FACTORY)))
		{
			$condition .= " AND cc.user_id=".$this->visitor->get('user_id');
		}
		$color_char_id=!empty($_REQUEST['color_char_id'])?$_REQUEST['color_char_id']:0;
		$color_attr_id=!empty($_REQUEST['color_attr_id'])?$_REQUEST['color_attr_id']:0;
		if (!empty($color_char_id)){
			$this->assign('color_arr_id',$color_char_id);
			$last_arr=array();
			foreach ($color_char_id as $rows){
				$cf_id=$rows;
				$cc_arr=$color_rel_mod->find(array(
					'conditions'=>'cf_id='.$cf_id.' and cs_id is null'		
				));
				foreach ($cc_arr as $c_rows){
					$cc_arr_id[]=$c_rows['cc_id'];
				}
			}
			if (!empty($cc_arr_id)){
				$count_arr=array_count_values($cc_arr_id);
				$result_arr=count($color_char_id);
				if (!empty($count_arr)){
					foreach ($count_arr as $k=>$val){
						if ($val==$result_arr){
							$last_arr[]=$k;
						}
					}
				}
			}
		}
		if (!empty($color_attr_id)){
			$this->assign('color_attr_id',$color_attr_id);
			$c_last_arr=array();
			foreach ($color_attr_id as $res){
				$cs_id=$res;
				$c_arr=$color_rel_mod->find(array(
						'conditions'=>'cs_id='.$cs_id.' and cf_id is null'
				));
				foreach ($c_arr as $c_res){
					$c_arr_id[]=$c_res['cc_id'];
				}
			}
			if (!empty($c_arr_id)){
				$c_count_arr=array_count_values($c_arr_id);
				$c_result_arr=count($color_attr_id);
				if (!empty($c_count_arr)){
					foreach ($c_count_arr as $j=>$cs_rows){
						if ($cs_rows==$c_result_arr){
							$c_last_arr[]=$j;
						}
					}
				}
			}
		}
		
		if (!empty($color_char_id) || !empty($color_attr_id)){
			if (!empty($color_char_id) && !empty($color_attr_id)){
				if (!empty($cc_arr_id) || !empty($c_arr_id)){
					$arr_id=array_unique($last_arr);
					$arr_id1=array_unique($c_last_arr);
					$cl_arr=array_intersect($arr_id,$arr_id1);
				}
			}else{
				if (!empty($cc_arr_id) && !empty($c_arr_id)){
					$arr_id=array_unique($last_arr);
					$arr_id1=array_unique($c_last_arr);
					$cl_arr=array_intersect($arr_id,$arr_id1);
				}elseif ($c_arr_id){
					$cl_arr=array_unique($c_last_arr);
				}elseif ($cc_arr_id){
					$cl_arr=$last_arr;
				}
			}
			$condition .=" and cc_id ".db_create_in($cl_arr);
		}
		
		//$condition .= " AND user_id={$this->visitor->get('user_id')}";
    	//echo $condition;
    	$colorcard_mode=& m('color_card');
    	if ($user['role']==USER_CLIENT){
    		$condition.=" and color_status!=0";
    	}
    	if ($user['role']==USER_MERCHANT){
    		$member_info=$member_mod->get(array(
    			'conditions'=>'fabric_shop.user_id='.$user['user_id'],
    			'join'=>'has_merchant'	
    		));
    		$condition.=" and (cc.user_id=".$user['user_id']." or cc.fabric_shop_id={$member_info['fabric_shop_id']})";
    	}
    	$colorcard_list = $colorcard_mode->find(array(
			'conditions'=>$condition, 
			'count' => true,
			'join'=>'has_member',
			'limit' => $page['limit'],
			'order'=>$order
    	));

    	//判断是否具有编辑权限
    	//$roles = array(USER_MERCHANDISER,USER_MANAGER,USER_CIVILIAN,USER_ASSISTANT,USER_MERCHANDISER_ADMIN,USER_FINANCE);   //具有编辑权限的role
    	foreach($colorcard_list as $key=>$cl){
    		$colorcard_list[$key]['edit'] = 0;
    		//($cl['user_id']==$this->visitor->get('user_id')||in_array($this->visitor->get('role'), $roles)) && $colorcard_list[$key]['edit']  = 1;
    		($cl['user_id']==$this->visitor->get('user_id')) && $colorcard_list[$key]['edit']  = 1;
    	}
    	//print_r($colorcard_list);
    	$page['item_count'] = $colorcard_mode->getCount();
    	$this->_format_page($page);
    	$this->assign('page_info', $page);
    	$this->assign('user',$user);
    	$this->assign('colorcard_list',$colorcard_list);
    	$this->display('colorcard.index.html');
    }
    
    //增加色卡
    function add() {
    	if(!$_POST){
			$this->assign('phpsision', session_id());
    		$fs_mod =& m('fabric_shop');
    		if ($this->visitor->get('role')==USER_MERCHANT){
    			$this->assign('fabricshop_list', $fs_mod->find(array('conditions'=>'if_show=1 and user_id='.$this->visitor->get('user_id'),'order'=>'fabric_shop_name ASC')));
    		}else{
    			$this->assign('fabricshop_list', $fs_mod->find(array('conditions'=>'if_show=1','order'=>'fabric_shop_name ASC')));
    		}
    		$color_char_mod=&m('color_property');
    		$color_char_list=$color_char_mod->find(array(
    			'conditions'=>'1=1',
    			'order'=>'cf_time desc'	
    		));
    		$color_attr_mod=&m('color_attr');
    		$color_attr_list=$color_attr_mod->find(array(
    				'conditions'=>'1=1',
    				'order'=>'cs_time desc'
    		));
    		$this->assign('color_char_list',$color_char_list);
    		$this->assign('color_attr_list',$color_attr_list);
    		$this->assign('user',$this->visitor->get());
    		$this->_curlocal(Lang::get('color_card'));
    		$this->display('colorcard.add.html');
    	}else{
    		$data = $this->_get_colorcard();
    		$colorcard_mode=&m('color_card');
    		if(!$cc_id=$colorcard_mode->add($data))
    		{
    			$this->show_warning('错误，添加出错','返回色卡列表','index.php?app=colorcard');
    		}
    		$upfiles=$_POST['upfiles'];
    		
    		if(!empty($upfiles))
    		{
    			$upload_mod =& m('upload');
    			$upload_mod->edit("file_id ".db_create_in($upfiles),"key_id={$cc_id}");
    			$file_info= $upload_mod->get(array(
					'conditions' => 'file_type = ' . BELONGS_COLORCARD_CC . ' AND key_id = ' . $cc_id,
					'order' => 'file_id ASC',
				));
    			$colorcard_mode->edit($cc_id,array('default_image'=>$file_info['file_path']));
    		}
    		$color_rel_mod=&m('color_relation');
    		$color_char_id=empty($_POST['color_char_id'])?0:$_POST['color_char_id'];
    		$color_attr_id=empty($_POST['color_attr_id'])?0:$_POST['color_attr_id'];
    		if (!empty($color_char_id)){
    			$cm_data=array();
    			if ($cc_id){
    				foreach ($color_char_id as $key=>$rows){
    					$cm_data['cc_id']=$cc_id;
    					$cf_id=$rows;
    					$cm_data['cf_id']=$cf_id;
    					$cm_info=$color_rel_mod->get(array(
    						'conditions'=>'cc_id='.$cc_id.' and cf_id='.$cf_id	
    					));
    					if (empty($cm_info)){
    						$color_rel_mod->add($cm_data);
    					}
    				}
    			}
    		}
    		
    		if (!empty($color_attr_id)){
    			$cs_data=array();
    			if ($cc_id){
    				foreach ($color_attr_id as $res){
    					$cs_data['cc_id']=$cc_id;
    					$cs_id=$res;
    					$cs_data['cs_id']=$cs_id;
    					$cs_info=$color_rel_mod->get(array(
    							'conditions'=>'cc_id='.$cc_id.' and cs_id='.$cs_id
    					));
    					if (empty($cs_info)){
    						$color_rel_mod->add($cs_data);
    					}
    				}
    			}
    		}
    		
    		$this->show_message('添加完成','编辑色卡','index.php?app=colorcard&act=index');
    	}
    }
    
    //删除色卡
    function del(){
    	$cc_id = $_REQUEST['cc_id']?intval($_REQUEST['cc_id']):'0';
    	if(!$cc_id){
    		$this->show_warning('错误，删除出错','返回色卡列表','index.php?app=colorcard');
    		return;
    	}
    	$colorcard_mode=&m('color_card');
    	$colorcard = $colorcard_mode->get_info($cc_id);
		
    	$file_mod =& m('upload');
		$colorcard_files = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_COLORCARD_CC . ' AND key_id = ' . $cc_id,
				'order' => 'file_id ASC',
		));
		
		foreach($colorcard_files as $item)
		{
		  if (!$file_mod->drop($item['file_id'])) {
			  $this->show_warning('删除文件失败');
			  return;
		  } else {
			  if (file_exists($item['file_path'])) {
				  unlink($item['file_path']);
			  }
		  }
		}
		
    	if($colorcard_mode->drop($cc_id)){
    		$color_relation_mod=&m('color_relation');
    		$color_relation_mod->drop("cc_id=".$cc_id);
    		if ($color_relation_mod->has_error()){
    			$this->show_warning($color_relation_mod->get_error());
    			return;
    		}
    		$this->show_message('删除完成','返回色卡列表','index.php?app=colorcard');
    	}
    }
    
    //重置色卡
    function reset_color(){
    	$cc_id = $_REQUEST['cc_id']?intval($_REQUEST['cc_id']):'0';
    	if(!$cc_id){
    		$this->show_warning('错误，重置出错','返回色卡列表','index.php?app=colorcard');
    		return;
    	}
    	$colorcard_mode=&m('color_card');
    	$colorcard = $colorcard_mode->get_info($cc_id);
    
    	if($colorcard['color_status']==1){
    		$colorcard_mode->edit($cc_id,array('color_status'=>0));
    	}
    	if ($colorcard_mode->has_error()){
    		$this->show_warning($colorcard_mode->get_error());
    		return;
    	}
    	$this->show_message('重置成功','返回色卡列表','index.php?app=colorcard');
    }
    
    //
    function edit(){
    	$user=$this->visitor->get();
    	$cc_id = $_REQUEST['cc_id']?intval($_REQUEST['cc_id']):'0';
    	if(!$cc_id){
    		$this->show_warning('不存在该色卡信息','返回色卡列表','index.php?app=colorcard');
    		return;
    	}
    	$this->assign('cc_id',$cc_id);
    	$colorcard_mode=&m('color_card');
    	$color_rel_mod=&m('color_relation');
    	$colorcard = $colorcard_mode->get_info($cc_id);
    	if (!in_array($user['role'], array(USER_MANAGER,USER_CIVILIAN))){
    		if ($colorcard['color_status']==1){
    			$this->assign('is_view',true);
    		}
    	}
    	if(!$_POST){
			$this->assign('phpsision', session_id());
			$file_mod =& m('upload');
			$colorcard_files = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_COLORCARD_CC . ' AND key_id = ' . $cc_id,
				'order' => 'file_id ASC',
			));
			
			$color_char_mod=&m('color_property');
			$color_char_list=$color_char_mod->find(array(
					'conditions'=>'1=1',
					'order'=>'cf_time desc'
			));
			$color_attr_mod=&m('color_attr');
			$color_attr_list=$color_attr_mod->find(array(
					'conditions'=>'1=1',
					'order'=>'cs_time desc'
			));
			$rel_cf_list=$color_rel_mod->find(array(
					'conditions'=>'cc_id='.$cc_id.' and cs_id is null'
			));
			$color_char_data=array();
			if (!empty($rel_cf_list)){
				foreach ($rel_cf_list as $cf_rows){
					$color_char_data[]=$cf_rows['cf_id'];
				}
			}
			
			$rel_cs_list=$color_rel_mod->find(array(
					'conditions'=>'cc_id='.$cc_id.' and cf_id is null'
			));
			$color_attr_data=array();
			if (!empty($rel_cs_list)){
				foreach ($rel_cs_list as $cs_rows){
					$color_attr_data[]=$cs_rows['cs_id'];
				}
			}
			$this->assign('color_char_arr',$color_char_data);
			$this->assign('color_attr_arr',$color_attr_data);
			$this->assign('color_char_list',$color_char_list);
			$this->assign('color_attr_list',$color_attr_list);
			$this->assign('colorcard_files',$colorcard_files);
	    	$fs_mod =& m('fabric_shop');
	    	if ($this->visitor->get('role')==USER_MERCHANT){
	    		$this->assign('fabricshop_list', $fs_mod->find(array('conditions'=>'if_show=1 and user_id='.$this->visitor->get('user_id'),'order'=>'fabric_shop_name ASC')));
	    	}else{
	    		$this->assign('fabricshop_list', $fs_mod->find(array('conditions'=>'if_show=1','order'=>'fabric_shop_name ASC')));
	    	}
	    	$this->assign('colorcard',$colorcard);
	    	$this->assign('user',$user);
	    	$this->display('colorcard.edit.html');
    	}else{
    		$data = $this->_get_colorcard();
    		$colorcard_mode->edit($cc_id,$data);
    		
    		if($colorcard_mode->has_error()){
    			$this->show_warning($colorcard_mode->get_error(),'返回色卡列表','index.php?app=colorcard');
    		}
    		
    		$color_char_id=empty($_POST['color_char_id'])?0:$_POST['color_char_id'];
    		$color_attr_id=empty($_POST['color_attr_id'])?0:$_POST['color_attr_id'];
    		if (!empty($color_char_id)){
    			$cm_data=array();
    			if ($cc_id){
    				$cm_list=$color_rel_mod->find(array(
    						'conditions'=>'cc_id='.$cc_id.' and cs_id is null'
    				));
    				$cm_data['cc_id']=$cc_id;
    				if (!empty($cm_list)){
    					foreach($cm_list as $cc_rows)
    					{
    						if(!in_array($cc_rows['cf_id'],$color_char_id))
    						{
    							$color_rel_mod->drop("cm_id={$cc_rows['cm_id']}");
    						}
    					}
    				}
    				foreach ($color_char_id as $key=>$rows){
    					$cf_id=$rows;
    					$cm_data['cf_id']=$cf_id;
    					$cm_info=$color_rel_mod->get(array(
    							'conditions'=>'cc_id='.$cc_id.' and cf_id='.$cf_id
    					));
    					if (empty($cm_info)){
    						$color_rel_mod->add($cm_data);
    					}
    				}
    			}
    		}
    		
    		if (!empty($color_attr_id)){
    			$cs_data=array();
    			if ($cc_id){
    				$cs_list=$color_rel_mod->find(array(
    						'conditions'=>'cc_id='.$cc_id.' and cf_id is null'
    				));
    				$cs_data['cc_id']=$cc_id;
    				if (!empty($cs_list)){
    					foreach($cs_list as $cs_rows)
    					{
    						if(!in_array($cs_rows['cs_id'],$color_attr_id))
    						{
    							$color_rel_mod->drop("cm_id={$cs_rows['cm_id']}");
    						}
    					}
    				}
    				foreach ($color_attr_id as $res){
    					$cs_id=$res;
    					$cs_data['cs_id']=$cs_id;
    					$cs_info=$color_rel_mod->get(array(
    							'conditions'=>'cc_id='.$cc_id.' and cs_id='.$cs_id
    					));
    					if (empty($cs_info)){
    						$color_rel_mod->add($cs_data);
    					}
    				}
    			}
    		}
    		
    		$this->show_message('编辑完成','编辑色卡','index.php?app=colorcard&act=index');
    	}
    }
    
    //获取post过来的色卡信息
    function _get_colorcard(){
    	$data=array(
    			'update_time' => $_REQUEST['update_time']?strtotime($_REQUEST['update_time']):'0',
    			'fabric_shop_id' => $_REQUEST['fabric_shop_id']?intval($_REQUEST['fabric_shop_id']):'0',
    			'item' => $_REQUEST['item']?trim($_REQUEST['item']):'0',
    			'price' => $_REQUEST['price']?floatval($_REQUEST['price']):'0.00',
    			'width' => $_REQUEST['width']?intval($_REQUEST['width']):'0',
    			'fabric_characteristics' => $_REQUEST['fabric_characteristics']?trim($_REQUEST['fabric_characteristics']):'',	
    	);
    	$color_status=$_REQUEST['color_status']?intval($_REQUEST['color_status']):'0';
    	if($_REQUEST['act']!='edit'){
    		$data['user_id'] = $this->visitor->get("user_id");
    	}
    	$fs_mod =& m('fabric_shop');
    	if($data['fabric_shop_id']){
    		$data['fabric_shop_name'] = $fs_mod->get_fs_name($data['fabric_shop_id']);
    	}
    	if ($color_status){
    		$data['color_status']=$color_status;
    	}else{
    		if (in_array($this->visitor->get('role'), array(USER_MANAGER,USER_MERCHANDISER_ADMIN,USER_CIVILIAN))){
    			$data['color_status']=0;
    		}
    	}
    	return $data;
    }
    
    //判断布行档口 品名货号 是否重复
    function check_fi(){
    	$fsi = $_REQUEST['fsi']?intval($_REQUEST['fsi']):'0';
    	$item = $_REQUEST['item']?trim($_REQUEST['item']):'';
    	$cc_id = $_REQUEST['cc_id']?intval($_REQUEST['cc_id']):'0'; //编辑的时候会传输id
    	if($fsi && $item){
    		$colorcard_mode=&m('color_card');
    		$where = null;  
    		$cc_id && $where = " AND cc_id!=".$cc_id; 
    		$sql = "SELECT COUNT(*) FROM ".$colorcard_mode->table." WHERE fabric_shop_id=".$fsi." AND item='".$item.$where."'";
    		$retval = $colorcard_mode->getOne($sql);
    		$this->json_result($retval,'','');
    		exit;
    	}else{
    		
    	}
    }
    
     function down()
   	{
		$id = $_REQUEST['id']?intval($_REQUEST['id']):'0';
		$type=isset($_GET['type'])?trim($_GET['type']):"cc";
    	if(!$id){
    		$this->show_warning('下载错误,未找到该色卡信息','返回首页','index.php');
    		return;
    	}
    	$info=NULL;
		
		if($type=="cc")
		{
    	$colorcard_mode=&m('color_card');
    	$info = $colorcard_mode->get_info($id);
		}
		else
		{
		$fabric_info_mod =& m('fabric_info');
		$info = $fabric_info_mod->get('fabric_info_id = ' . $id);
		}
		
		if(empty($info)){
    		$this->show_warning('下载错误,未找到该色卡信息','返回首页','index.php');
    		return;
    	}
		$down_type=$type=="cc"?"cc":"fab";
		$sn=(time()+10050)*2;
		header("location:downfile.php?sn={$sn}&type={$down_type}&id=" . $id);
	
   }
   
   	function _upload_cc($belongs,$id)
	{
		if (!$belongs || !$id) {
                    if(IS_AJAX){
                        $this->json_error(Lang::get('system_error'));
                        exit;
                    }
			echo "<script type='text/javascript'>alert('" . Lang::get('system_error') . "');window.parent.unblock();</script>";
			exit;
		}
		
		if (!IS_POST)
	    {
		} 
		else 
		{
			$arr_file = $_FILES['files'];
			$files = array();
			foreach ($arr_file['name'] as $key => $name) {
				$files[$key]['name'] = $arr_file['name'][$key];
				$files[$key]['type'] = $arr_file['type'][$key];
				$files[$key]['tmp_name'] = $arr_file['tmp_name'][$key];
				$files[$key]['size'] = $arr_file['size'][$key];
				$files[$key]['error'] = $arr_file['error'][$key];
			}
			
			/* 文件保存路径 */
			$dirname = '';
			$upload_files_id = '';
			$dirname = 'data/files/colorcard/' . $id;
			$upload_file_id = 'colorcard_files';
			
			$file_path = array();
			foreach ($files as $key => $file) {
				if ($file['error'] != UPLOAD_ERR_NO_FILE) {
					$file_path[$key]['file_path'] = $this->_upload_docs($belongs, $id, $file, $dirname);
				}
			}
			return $file_path;
		}
	}
   
   	function _upload_docs($belongs, $id, $file, $dirname, $is_img = false)
	{
            import('image.func');
            import('uploader.lib');
            $uploader = new Uploader();
            $is_img ? $uploader->allowed_type(IMAGE_FILE_TYPE) : $uploader->allowed_type(DOC_FILE_TYPE);
            $uploader->allowed_size(SIZE_DOC); // 5M
            $upload_mod =& m('uploadedfile');
			
                /* 处理文件上传 */
                $addfile = array(
                    'name'      => $file['name'],
                    'type'      => $file['type'],
                    'tmp_name'  => $file['tmp_name'],
                    'size'      => $file['size'],
                    'error'     => $file['error']
                );
                $uploader->addFile($addfile);
                if (!$uploader->file_info())
                {
                    $data = current($uploader->get_error());
                    $res = Lang::get($data['msg']);
                    echo "<script type='text/javascript'>alert('{$res}');window.parent.unblock();</script>";
                    exit;
                }
				
                $uploader->root_dir(ROOT_PATH);
                $filename  = $uploader->random_filename();
                $file_path = $uploader->save($dirname, $filename);
				
				/* 将文件信息写入数据库 */
				$upload_mod =& m('upload');
				$data = array(
					'file_name' => $addfile['name'],
					'file_path' => $file_path,
					'file_type' => $belongs,
					'key_id' => $id
				);
				$file_id = $upload_mod->add($data);
				
				return $file_path;
	}
	
	function show(){
    	$cc_id = $_REQUEST['cc_id']?intval($_REQUEST['cc_id']):'0';
    	if(!$cc_id){
    		$this->show_warning('不存在该色卡信息','返回色卡列表','index.php?app=colorcard');
    		return;
    	}
    	$this->assign('cc_id',$cc_id);
    	$colorcard_mode=&m('color_card');
    	$colorcard = $colorcard_mode->get_info($cc_id);
		$file_mod =& m('upload');
		$colorcard_files = $file_mod->find(array(
			'conditions' => 'file_type = ' . BELONGS_COLORCARD_CC . ' AND key_id = ' . $cc_id,
			'order' => 'file_id ASC',
		));
		$this->assign('colorcard_files',$colorcard_files);
		$fs_mod =& m('fabric_shop');
		$this->assign('fabricshop_list', $fs_mod->find(array('conditions'=>'if_show=1','order'=>'fabric_shop_name ASC')));
		$this->assign('colorcard',$colorcard);
		$this->display('colorcard.show.html');
    }
    
    
    //面料特征查询列表
    function color_characteristics(){
    	$page = $this->_get_page();
    	$colorcard_mode=&m('color_property');
    	$color_list = $colorcard_mode->find(array(
    			'join'=>'has_member',
    			'conditions' => '1=1',
    			'count' => true,
    			'limit' => $page['limit']
    	));
    	$page['item_count'] = $colorcard_mode->getCount();
    	$this->_format_page($page);
    	$this->assign('page_info', $page);
    	$this->assign('color_list', $color_list);
    	$this->display('color_char.list.html');
    	$this->_curlocal(Lang::get('color_char_list'));
    }
    
    
    //添加面料特征
    function color_char_add(){
    	$colorcard_mode=&m('color_property');
    	if(!$_POST){
    		$this->_curlocal(Lang::get('color_char_list'), 'index.php?app=colorcard&act=color_characteristics',
    				Lang::get('add_color_char'));
    		$this->display('color_char.form.html');
    	}else{
    		$data=array();
    		$cf_name=trim($_POST['cf_name']);
    		if (!$cf_name){
    			$this->show_warning('cf_name_not_empty');
    			return;
    		}
    		$data['cf_characteristics']=$cf_name;
    		$data['cf_time']=gmtime();
    		$data['user_id']=$this->visitor->get('user_id');
    		$colorcard_mode->add($data);
    		if ($colorcard_mode->has_error()){
    			$this->show_warning($colorcard_mode->get_error());
    			return;
    		}
    		$this->show_message('添加成功',Lang::get('color_char_list'),'index.php?app=colorcard&act=color_characteristics');
    	}
    }
    
    
    //编辑面料特征
    function color_char_edit(){
    	$colorcard_mode=&m('color_property');
    	$cf_id=isset($_GET['cf_id'])?$_GET['cf_id']:0;
    	if (!$cf_id){
    		$this->show_warning("没有该面料特征");
    		return;
    	}
    	if(!$_POST){
    		$color_info=$colorcard_mode->get($cf_id);
    		$this->_curlocal(Lang::get('color_char_list'), 'index.php?app=colorcard&act=color_characteristics',
    				Lang::get('edit_color_char'));
    		$this->assign('colors',$color_info);
    		$this->display('color_char.form.html');
    	}else{
    		$data=array();
    		$cf_name=trim($_POST['cf_name']);
    		if (!$cf_name){
    			$this->show_warning('cf_name_not_empty');
    			return;
    		}
    		$data['cf_characteristics']=$cf_name;
    		$data['user_id']=$this->visitor->get('user_id');
    		$colorcard_mode->edit($cf_id,$data);
    		if ($colorcard_mode->has_error()){
    			$this->show_warning($colorcard_mode->get_error());
    			return;
    		}
    		$this->show_message('编辑成功',Lang::get('color_char_list'),'index.php?app=colorcard&act=color_characteristics');
    	}
    }
    
   //删除面料特征
   function color_char_drop(){
	   	$colorcard_mode=&m('color_property');
	   	$cf_id=isset($_GET['cf_id'])?$_GET['cf_id']:0;
	   	if (!$cf_id){
	   		$this->show_warning("没有该面料特征");
	   		return;
	   	}
	   	$colorcard_mode->drop($cf_id);
	   	if ($colorcard_mode->has_error()){
	   		$this->show_warning($colorcard_mode->get_error());
	   		return;
	   	}
	   	$color_relation_mod=&m('color_relation');
	   	$color_relation_mod->drop("cf_id=".$cf_id);
	   	if ($color_relation_mod->has_error()){
	   		$this->show_warning($color_relation_mod->get_error());
	   		return;
	   	}
	   	$this->show_message('删除成功');
   }
   
   
   //面料特征颜色属性列表
   function color_attr(){
	   	$page = $this->_get_page();
	   	$colorcard_mode=&m('color_attr');
	   	$color_list = $colorcard_mode->find(array(
	   			'join'=>'has_member',
	   			'conditions' => '1=1',
	   			'count' => true,
	   			'limit' => $page['limit']
	   	));
	   	$page['item_count'] = $colorcard_mode->getCount();
	   	$this->_format_page($page);
	   	$this->assign('page_info', $page);
	   	$this->assign('color_list', $color_list);
	   	$this->display('color_attr.list.html');
	   	$this->_curlocal(Lang::get('color_attr_list'));
   }
   
   
   //添加特征属性
   function color_attr_add(){
	   	$colorcard_mode=&m('color_attr');
	   	if(!$_POST){
	   		$this->_curlocal(Lang::get('color_attr_list'), 'index.php?app=colorcard&act=color_attr',
	   				Lang::get('add_color_attr'));
	   		$this->display('color_attr.form.html');
	   	}else{
	   		$data=array();
	   		$cs_name=trim($_POST['cs_name']);
	   		if (!$cs_name){
	   			$this->show_warning('cs_name_not_empty');
	   			return;
	   		}
	   		$data['cs_name']=$cs_name;
	   		$data['cs_time']=gmtime();
	   		$data['user_id']=$this->visitor->get('user_id');
	   		$colorcard_mode->add($data);
	   		if ($colorcard_mode->has_error()){
	   			$this->show_warning($colorcard_mode->get_error());
	   			return;
	   		}
	   		$this->show_message('添加成功',Lang::get('color_attr_list'),'index.php?app=colorcard&act=color_attr');
	   	}
   }
   
   
   //编辑特征属性
   function color_attr_edit(){
   	$colorcard_mode=&m('color_attr');
   	$cs_id=isset($_GET['cs_id'])?$_GET['cs_id']:0;
   	if (!$cs_id){
   		$this->show_warning("没有该特征属性");
   		return;
   	}
   	if(!$_POST){
   		$color_info=$colorcard_mode->get($cs_id);
   		$this->_curlocal(Lang::get('color_attr_list'), 'index.php?app=colorcard&act=color_attr',
   				Lang::get('edit_color_attr'));
   		$this->assign('colors',$color_info);
   		$this->display('color_attr.form.html');
   	}else{
   		$data=array();
   		$cs_name=trim($_POST['cs_name']);
   		if (!$cs_name){
   			$this->show_warning('cs_name_not_empty');
   			return;
   		}
   		$data['cs_name']=$cs_name;
   		$data['user_id']=$this->visitor->get('user_id');
   		$colorcard_mode->edit($cs_id,$data);
   		if ($colorcard_mode->has_error()){
   			$this->show_warning($colorcard_mode->get_error());
   			return;
   		}
   		$this->show_message('编辑成功',Lang::get('color_attr_list'),'index.php?app=colorcard&act=color_attr');
   	}
   }
   
   
   //删除特征属性
   function color_attr_drop(){
	   	$colorcard_mode=&m('color_attr');
	   	$cs_id=isset($_GET['cs_id'])?$_GET['cs_id']:0;
	   	if (!$cs_id){
	   		$this->show_warning("没有该特征属性");
	   		return;
	   	}
	   	$colorcard_mode->drop($cs_id);
	   	if ($colorcard_mode->has_error()){
	   		$this->show_warning($colorcard_mode->get_error());
	   		return;
	   	}
	   	$color_relation_mod=&m('color_relation');
	   	$color_relation_mod->drop("cs_id=".$cs_id);
   		if ($color_relation_mod->has_error()){
	   		$this->show_warning($color_relation_mod->get_error());
	   		return;
	   	}
	   	$this->show_message('删除成功');
	}
	
	function recomm_color(){
		$cc_id=!empty($_GET['cc_id'])?$_GET['cc_id']:0;
		$sample_mod=&m('sample');
		$order_mod=&m('order');
		if (!IS_POST){
			$sample_list=$sample_mod->find(array(
					'conditions'=>'status!='.SAMPLE_STATUS_DELETE,
					'fields'=>'sample_sn',
					'order'=>'sample_sn asc'
			));
			$order_list=$order_mod->find(array(
					'conditions'=>'status!='.ORDER_STATUS_NO_CANCEL,
					'fields'=>'order_sn',
					'order'=>'order_sn asc'
			));
			$this->assign('sample_list',$sample_list);
			$this->assign('order_list',$order_list);
			$this->display('colorcard.recomm.html');
			$this->_curlocal(Lang::get('colorcard_manage'), 'index.php?app=colorcard', Lang::get('recomm_colorcard'));
		}else{
			$sample_id=$_POST['sample_id'];
			$order_id=$_POST['order_id'];
			if (!$sample_id && !$order_id){
				$this->show_warning("请选择样板/订单号");
				return;
			}
			if ($sample_id){
				$sample_info=$sample_mod->get_info($sample_id);
				if (empty($sample_info['recomm_cc_id'])){
					$sample_mod->edit($sample_id,array('recomm_cc_id'=>$cc_id));
				}else{
					if (!in_array($cc_id, explode(',', $sample_info['recomm_cc_id']))){
						$recomm_cc_id=$sample_info['recomm_cc_id'].','.$cc_id;
					}else{
						$recomm_cc_id=$sample_info['recomm_cc_id'];
					}
					$sample_mod->edit($sample_id,array('recomm_cc_id'=>$recomm_cc_id));
				}
			}
			
			if ($order_id){
				$order_info=$order_mod->get_info($order_id);
				if (empty($order_info['recomm_cc_id'])){
					$order_mod->edit($order_id,array('recomm_cc_id'=>$cc_id));
				}else{
					if (!in_array($cc_id, explode(',', $order_info['recomm_cc_id']))){
						$recomm_arr_id=$order_info['recomm_cc_id'].','.$cc_id;
					}else{
						$recomm_arr_id=$order_info['recomm_cc_id'];
					}
					$order_mod->edit($order_id,array('recomm_cc_id'=>$recomm_arr_id));
				}
			}
			if ($sample_mod->has_error()){
				$this->show_warning($sample_mod->get_error());
				return;
			}
			if ($order_mod->has_error()){
				$this->show_warning($order_mod->get_error());
				return;
			}
			$this->show_message("推荐成功！");
		}
	}
	
	//推荐色卡查看
	function recomm_view(){
		$sample_id=!empty($_GET['sample_id'])?$_GET['sample_id']:0;
		if (!$sample_id){
			$this->show_warning('sample_not_exist');
			return;
		}
		$sample_mod=&m('sample');
		$colorcard_mod=&m('color_card');
		$sample=$sample_mod->get_info($sample_id);
		if ($sample['recomm_cc_id']){
			$recomms=$colorcard_mod->find(array(
				'conditions' => 'cc_id '.db_create_in($sample['recomm_cc_id']),
				'order' => 'cc_id ASC',	
				'fields'=>'default_image'
			));
		}
		$this->assign('recomm_img',$recomms);
		$this->assign('sample_id',$sample_id);
		$this->assign('user',$this->visitor->get());
		$this->display('sample.recomm_color.html');
	}
	
	
	//移除推荐色卡
	function remove(){
		$cc_id = !empty($_GET['cc_id'])?$_GET['cc_id']:0;
		$sample_id=!empty($_GET['sample_id'])?$_GET['sample_id']:0;
		if (!$sample_id){
			$this->show_warning('sample_not_exist');
			return;
		}
		if(!$cc_id){
			$this->show_warning('color_not_exist');
			return;
		}
		$sample_mod=&m('sample');
		$sample=$sample_mod->get_info($sample_id);
		if ($sample['recomm_cc_id']){
			$recomm_id=explode(',', $sample['recomm_cc_id']);
			foreach ($recomm_id as $k=>$v){
				if ($v==$cc_id){
					unset($recomm_id[$k]);
				}
			}
			$recomm_arr_id=implode(',', array_filter($recomm_id));
			$sample_mod->edit($sample_id,array('recomm_cc_id'=>$recomm_arr_id));
			if ($sample_mod->has_error()){
				$this->show_warning($sample_mod->get_error());
				return;
			}
		}
		$this->show_message("remove_success");
	}
}
?>
